package sistemioperativi.prodcons.sample;

import java.util.concurrent.Semaphore;

public class BufferListIllimitato extends BufferList{
	
	private Semaphore permessiGet = new Semaphore(0);
	private Semaphore mutex = new Semaphore(1);
	
	
	
	public BufferListIllimitato(){
		super();
	}
	
	
	public void put(Elemento e) throws InterruptedException {
		
		mutex.acquire();
		System.out.println(Thread.currentThread().getId()+" PUT di "+e.getValore());
		this.coda.addLast(e);
		System.out.println("Carico: "+this.coda.size());
		permessiGet.release();
		mutex.release();
		
	}

	
	public Elemento get() throws InterruptedException {
		
		permessiGet.acquire();
		mutex.acquire();
		Elemento e = this.coda.removeFirst();
		System.out.println(Thread.currentThread().getId()+" GET di "+e.getValore());
		System.out.println("Carico: "+this.coda.size());
		mutex.release();
		
		
		return e;
	}
	
public static void main (String []args){
		
	
		BufferList buffer = new BufferListIllimitato();
		int numProduttori = 4;
		int numConsumatori = 6;
		
		buffer.test(numProduttori, numConsumatori);
	}
	

}
